1494D - Dogeforces - CodeForces Solution


constructive algorithms data structures dfs and similar divide and conquer dsu greedy sortings trees *2300

Please click on ads to support us..

C++ Code:

#include <bits/stdc++.h>
using namespace std;
const int N=1e5+10;

int p[N], v[N], ip[N];

int find(int x){
	if(x!=p[x]) return p[x]=find(p[x]);
	return x;
}


int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(0);

	#ifndef ONLINE_JUDGE	
	freopen("in.txt", "r", stdin);
	freopen("out.txt", "w", stdout);
	#endif

	int n; cin >> n;

	vector<array<int, 3>> q;

	for(int i = 0; i < n; i++){
		for(int l = 0; l < n; l++){
			int x; cin >> x;

			if(i == l) v[i] = x;
			else{
				q.push_back({x, i, l});
			}

		}
	}

	int cnt=n;


	sort(q.begin(), q.end());

	for(int i=0; i<1e5; i++) p[i]=i;

	for(int i=0; i<q.size(); i++){

		int val=q[i][0], x=q[i][1], y=q[i][2];
		x=find(x); y=find(y);

		if(v[x] >= val){
			p[y]=x;
			if(v[y]<val) ip[y]=x;
			continue;
		}
		else if(v[y]>=val){
			p[x]=p[y];
			if(v[x]<val) ip[x]=y;
			continue;
		}
		else if(v[y]<val&&v[x]<val){
			ip[x] = cnt;
			ip[y] = cnt;
			p[x] = p[y] = cnt;
			v[cnt] = val;
			p[cnt]=cnt;


			cnt++;
		}
	}

	cout<<cnt<<"\n";

	for(int i=0; i<cnt; i++) cout<<v[i]<<" ";

	cout<<"\n";

	cout<<cnt<<"\n";
	
	for(int i=0; i<cnt-1; i++)
	{
		cout<<i+1<<" "<<ip[i]+1<<"\n";
	}


}														


Comments

Submit
0 Comments
More Questions

WLDRPL Wildcard Replacement
1221. Split a String in Balanced Strings
1002. Find Common Characters
1602A - Two Subsequences
1555A - PizzaForces
1607B - Odd Grasshopper
1084A - The Fair Nut and Elevator
1440B - Sum of Medians
1032A - Kitchen Utensils
1501B - Napoleon Cake
1584B - Coloring Rectangles
1562B - Scenes From a Memory
1521A - Nastia and Nearly Good Numbers
208. Implement Trie
1605B - Reverse Sort
1607C - Minimum Extraction
1604B - XOR Specia-LIS-t
1606B - Update Files
1598B - Groups
1602B - Divine Array
1594B - Special Numbers
1614A - Divan and a Store
2085. Count Common Words With One Occurrence
2089. Find Target Indices After Sorting Array
2090. K Radius Subarray Averages
2091. Removing Minimum and Maximum From Array
6. Zigzag Conversion
1612B - Special Permutation
1481. Least Number of Unique Integers after K Removals
1035. Uncrossed Lines